export default {
	data() {
		return {
			searchStr: '',
			filterType: 0, // 排序or过滤：销量-0 上新-1 秒杀-2
			proList: []
		}
	},
	async onLoad(options) {
		console.log("products index onLoad: ", options);
		this.filterType = parseInt(options.filterType || 0);
		this.searchStr = options.filterStr || '';
		if (this.$store.state.login.token) {
			await this.getProducts();
		}
	},
	async onShow() {},
	onHide() {},
	async onPullDownRefresh() {
		if (this.$store.state.login.token) {
			await this.getProducts();
		}
		uni.stopPullDownRefresh();
	},
	methods: {
		// 获取产品列表
		async getProducts() {
			const productsRes = await this.$uniCloudCallFunction({
				name: 'xxf-product',
				action: "getProductList",
				data: {
					uid: this.$store.state.login.uid,
					searchStr: this.searchStr,
					page: 1,
					limit: 100
				}
			});
			console.log("productsRes: ", productsRes);
			if (productsRes) {
				this.proList = productsRes.list || [];
				// this.proList = [...productsRes.list, ...productsRes.list, ...productsRes.list];
			}
		},
		// 前往产品详情
		toDetail(id) {
			uni.navigateTo({
				url: `/pages/homePack/proDetail/index?id=${id}`,
			})
		},
		// 关键字搜索
		async onSearch() {
			if (this.searchStr) {
				await this.getProducts();
			}
		},
		// 筛选
		async filterTypeChange(type) {
			this.filterType = type;
			await this.getProducts();
		},
		async openSpuPop(e) {
			console.log("openSpuPop: ", e);
			this.$refs.spuRef.popupOpen({
				type: "add",
				productInfo: e
			});
		},
		popupChange(e) {
			this.popupShow = e.show;
		},
		async toCart(e) {
			const addRes = await this.$uniCloudCallFunction({
				name: 'xxf-cart',
				action: "addToCart",
				data: {
					uid: this.$store.state.login.uid,
					product_info: e.product_info,
					spu_info: e.spu_info,
					count: e.count,
					status: '1'
				}
			});
			console.log("addRes: ", addRes);
			await this.$utils.showToast(this, {
				message: '已经加入购物车！',
				mode: 1
			});
		},
		toBooking(e) {
			const proList = [{
				cartId: '',
				productId: e.productId,
				spuId: e.spuId,
				count: e.count,
			}];
			uni.navigateTo({
				url: `/pages/homePack/booking/index?proList=${JSON.stringify(proList)}`
			});
		}
	},
}